/* 
 *  Copyright (C) 2006 James Young
 * `gurumind(a)gmail.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

namespace IPDEditor
{
    public class IPDFile
    {
        string fileName;
        IPDDocument _doc;

        public IPDFile(string FileName)
        { 
            fileName = FileName;
        }

        public string FileNameShort
        {
            get
            {
                int pos = fileName.LastIndexOf("\\");
                string sfn = fileName.Substring(pos + 1);
                return sfn;
            }
        }

        public void Open()
        {
            FileStream fs = File.OpenRead(fileName);
            BinaryReader r = new BinaryReader(fs);

            _doc = new IPDDocument(this);
            _doc.Read(r);

            r.Close();
            fs.Close();
        }

        public void Import(string ExFileName)
        {
            FileStream fs = File.OpenRead(ExFileName);
            BinaryReader r = new BinaryReader(fs);

            IPDDocument exdoc = new IPDDocument(null);
            exdoc.Read(r);
            
            r.Close();
            fs.Close();

            //do merge
            foreach (IPDName exitem in exdoc.Items)
            {
                //look for target place
                bool havePlace = false;
                for (int i = 0; i < _doc.Items.Count; i++)
                {
                    IPDName target = _doc.Items[i];
                    if (target.name == exitem.name)
                    {
                        //it's here
                        foreach (IPDDatabase db in exitem.Items)
                        {
                            target.Items.Add(db);
                        }
                        havePlace = true;
                        break;
                    }
                }
                if (!havePlace)
                {
                    //no place to insert item. so create new
                    _doc.Items.Add(exitem);
                }
            }
        }

        public void SaveAs(string FileName)
        {
            FileStream _fs = File.OpenWrite(FileName);
            BinaryWriter w = new BinaryWriter(_fs);
            w.BaseStream.SetLength(0L);
            w.Flush();
            w.Seek(0, SeekOrigin.Begin);

            //tnBind.TreeView.SuspendLayout();
            _doc.Write(w);
            //tnBind.TreeView.ResumeLayout();

            w.Close();
            _fs.Close();

            fileName = FileName;
        }

        public void Save()
        {
            SaveAs(fileName);
        }

        public IPDDocument document
        {
            get
            {
                return _doc;
            }
        }

        public string FileName
        {
            get
            {
                return fileName;
            }
        }
    }
}
